﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Normal_TemplateListController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index(string actionId)
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回模板详细信息列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            int tempInt=0;
            //返回json数组
            if (!int.TryParse( actionId,out tempInt))
            {
                QueryParam qp = new QueryParam();
                qp.PageIndex = 1;
                qp.PageSize = 100;
                qp.Where = "TemplateName like '%" + actionId + "%' ";
                int recordNum = 0;
                IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
                IDataService<Normal_TemplateListEntity> Normal_TemplateListSv = ServiceBuilder.BuildNormal_TemplateListService();
                List<Normal_TemplateListEntity> normal_templatelistList = Normal_TemplateListSv.List(qp, out recordNum);
                Normal_TemplateListEntity tmp = new Normal_TemplateListEntity();
                tmp.Id = 0;
                tmp.TemplateTitle = "默认模板";
                normal_templatelistList.Add(tmp);
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_templatelistList, Formatting.Indented, jsDataConvert), "}");
            }
            else
            {
                int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
                int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
                QueryParam qp = new QueryParam();
                qp.PageIndex = pageIndex / pageSize + 1;
                qp.PageSize = pageSize;
                qp.Where = "TemplateId=" + actionId;
                int recordNum = 0;
                IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
                IDataService<Normal_TemplateListEntity> Normal_TemplateListSv = ServiceBuilder.BuildNormal_TemplateListService();
                List<Normal_TemplateListEntity> normal_templatelistList = Normal_TemplateListSv.List(qp, out recordNum);
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_templatelistList, Formatting.Indented, jsDataConvert), "}");
        
            }
            return returnJson;
 
        }
       

        /// <summary>
        /// 查看模板详细信息 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Normal_TemplateListEntity> Normal_TemplateListSv = ServiceBuilder.BuildNormal_TemplateListService();
           int intId = Utils.StringToInt(actionId, 0);
            Normal_TemplateListEntity normal_templatelist = Normal_TemplateListSv.Look(intId);
            if (normal_templatelist != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(normal_templatelist), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 查看模板详细信息 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look2(string actionId)
        {
            IDataService<Normal_TemplatesEntity>  Normal_TemplatesSv=ServiceBuilder.BuildNormal_TemplatesService();
            IDataService<Normal_TemplateListEntity> Normal_TemplateListSv = ServiceBuilder.BuildNormal_TemplateListService();
            int intId = Utils.StringToInt(actionId, 0);
            Normal_TemplateListEntity normal_templatelist = Normal_TemplateListSv.Look(intId);
            Normal_TemplatesEntity normal_tmeplates=Normal_TemplatesSv.Look(normal_templatelist.TemplateId);
             string allPath=string.Concat( Utils.GetServerPath() , normal_tmeplates.Dir,"\\",normal_templatelist.TemplateName);
           
           string templateCongent = Utils.ReadFile(allPath, System.Text.Encoding.GetEncoding("gb2312"));
            if (normal_templatelist != null)
                returnJson = string.Concat("{success:true,data:{\"Id\":", normal_templatelist.Id, ",\"TemplateName\":\"", normal_templatelist.TemplateName, "\",\"TemplateContent\":", JsonConvert.SerializeObject(templateCongent), "}}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        
        /// <summary>
        /// 更新模板详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
         [ValidateInput(false)]
        public string Update2(string actionId)
        {
            IDataService<Normal_TemplatesEntity> Normal_TemplatesSv = ServiceBuilder.BuildNormal_TemplatesService();
            IDataService<Normal_TemplateListEntity> Normal_TemplateListSv = ServiceBuilder.BuildNormal_TemplateListService();
            int intId = Utils.StringToInt(actionId, 0);
            Normal_TemplateListEntity normal_templatelist = Normal_TemplateListSv.Look(intId);
            Normal_TemplatesEntity normal_tmeplates = Normal_TemplatesSv.Look(normal_templatelist.TemplateId);
            string allPath = string.Concat(Utils.GetServerPath(), normal_tmeplates.Dir, "\\", normal_templatelist.TemplateName);
            string templateContent = (String)Utils.GetQueryAndFormString("TemplateContent", this, 20000, 1, DataType.Str);

            Utils.BackupFile(allPath, allPath + ".tmp1");
            if (Utils.SaveFile(allPath, templateContent, false, System.Text.Encoding.GetEncoding("gb2312")))
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新模板详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Normal_TemplateListEntity normal_templatelist = new Normal_TemplateListEntity();
                normal_templatelist.DataTable_Action = DataTableAction.Update;
                normal_templatelist.Id = intId;
                normal_templatelist.TemplateId = (Int32)Utils.GetQueryAndFormString("TemplateId", this, 10,  1 , DataType.Int);
                normal_templatelist.ModuleType = (String)Utils.GetQueryAndFormString("ModuleType", this, 32,  1 , DataType.Str);
                normal_templatelist.TemplateName = (String)Utils.GetQueryAndFormString("TemplateName", this, 30,  1 , DataType.Str);
                normal_templatelist.TemplateTitle = (String)Utils.GetQueryAndFormString("TemplateTitle", this, 20, 1, DataType.Str);
                normal_templatelist.Enabled = (bool)Utils.GetQueryAndFormString("Enabled", this, 6, 1, DataType.Bool);
                normal_templatelist.IsDefault = (bool)Utils.GetQueryAndFormString("IsDefault", this, 6, 1, DataType.Bool);
                IDataService<Normal_TemplateListEntity> Normal_TemplateListSv = ServiceBuilder.BuildNormal_TemplateListService();
                if (Normal_TemplateListSv.Update(normal_templatelist) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增模板详细信息
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Normal_TemplateListEntity normal_templatelist = new Normal_TemplateListEntity();
            normal_templatelist.DataTable_Action = DataTableAction.Insert;
            normal_templatelist.TemplateId = (Int32)Utils.GetQueryAndFormString("TemplateId", this, 10,  1 , DataType.Int);
            normal_templatelist.ModuleType = (String)Utils.GetQueryAndFormString("ModuleType", this, 32, 1, DataType.Str);
            normal_templatelist.TemplateName = (String)Utils.GetQueryAndFormString("TemplateName", this, 30, 1, DataType.Str);
            normal_templatelist.Enabled = (bool)Utils.GetQueryAndFormString("Enabled", this,6,  1, DataType.Bool);
            normal_templatelist.TemplateTitle = (String)Utils.GetQueryAndFormString("TemplateTitle", this, 20, 1, DataType.Str);
            normal_templatelist.IsDefault = (bool)Utils.GetQueryAndFormString("IsDefault", this, 6, 1, DataType.Bool);
           IDataService<Normal_TemplateListEntity> Normal_TemplateListSv = ServiceBuilder.BuildNormal_TemplateListService();
           if (Normal_TemplateListSv.Insert(normal_templatelist) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除模板详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Normal_TemplateListEntity normal_templatelist = new Normal_TemplateListEntity();
                normal_templatelist.Id = intId;
                IDataService<Normal_TemplateListEntity> Normal_TemplateListSv = ServiceBuilder.BuildNormal_TemplateListService();
                if (Normal_TemplateListSv.Delete(normal_templatelist) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除模板详细信息
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Normal_TemplateListEntity> Normal_TemplateListSv = ServiceBuilder.BuildNormal_TemplateListService();
                if (Normal_TemplateListSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
